# pip install objgraph
import time
import threading

import numpy
import objgraph
# pip install pympler
from pympler import muppy, summary, tracker

import tracemalloc

tracemalloc.start(25)


class MemoryTest(object):
    def __init__(self):
        self.memory_list = []
        threading.Thread(target=self.print_memory_used).start()

    def add_memory(self):
        np_a = numpy.array((3, 3))
        while True:
            self.memory_list.append(np_a)
            # time.sleep(0.1)

    def print_memory_used(self):
        snapshot = tracemalloc.take_snapshot()
        while True:
            try:
                snapshot1 = tracemalloc.take_snapshot()
                top_stats = snapshot1.compare_to(snapshot, 'lineno')
                # logger.warning("[ Top 20 differences ]")
                for stat in top_stats[:20]:
                    if stat.size_diff < 0:
                        continue
                    # logger.warning(stat)
                    print(stat)
                print("---------------------------")
                # snapshot = tracemalloc.take_snapshot()

            except Exception as e:
                print(e)
                pass

            time.sleep(10)


if __name__ == '__main__':
    memory_test = MemoryTest()
    memory_test.add_memory()
